/**
 * utils/get-client-by-ua
 */

const lodash = require('lodash');

const clients = require('../data/clients.js');

const rAndroid = /Android/i;
const rIOS = /\(i[^;]+;( U;)? CPU.+Mac OS X/i;
const rVersion = /(?:xiaoshibing?|xsb)(?:-dev)?\/([\w.]+)/i;

module.exports = function(ua) {
    let client = clients.WEB;
    client.os = clients.WEB.name;
    if(ua && rIOS.test(ua)) {
        client = clients.IOS;
        client.os = clients.IOS.name;
    }
    else if(ua && rAndroid.test(ua)) {
        client = clients.ANDROID;
        client.os = clients.ANDROID.name;
    }

    // clone
    client = lodash.cloneDeep(client);

    // version
    client.version = '0.0.1';
    if(rVersion.test(ua)) {
        client.version = RegExp.$1;
    }

    return client;
};
